﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class ChucNang_NguoiDung_NguoiDung_KetQuaTimKiem : System.Web.UI.Page
{
    int page, rowperpage;
    protected void Page_Load(object sender, EventArgs e)
    {
        
        page = 0;
        rowperpage = 5;
        if (Request["page"] != null)
            page = int.Parse(Request["page"]);
        if (Request["rowperpage"] != null)
            rowperpage = int.Parse(Request["rowperpage"]);
        //gia tri trong application
        //int RowPerPage = (int)Application["RowPerPage"];
        int hienthi = (int)Session["HienThi"];
        if (!this.IsPostBack)
        {
            ddlPhanTrang.SelectedValue = rowperpage.ToString();
            ddlThuTu.SelectedValue = hienthi.ToString();
        }
        //gia tri trong web.config
        int RowPerPage = rowperpage;
        int startIndex = (page * RowPerPage);
        int TongSoDong = (int)Session["TongSo"];
        LBLSOLUONG.Text = "Số lượng kết quả tìm được: "+ TongSoDong.ToString();
        PageInterface1.currentPageName = GlobalFunctions.GetCurrentPageName();
        PageInterface1.currentPageNumber = page;
        PageInterface1.rowPerPage = rowperpage;
        PageInterface1.totalNumberOfPage = TongSoDong / RowPerPage;
        if (TongSoDong % RowPerPage != 0)
            PageInterface1.totalNumberOfPage++;

        //
        string query=(string)Session["TimKiem"];
        query=query.Replace("@Start",startIndex.ToString());
        query=query.Replace("@Num",RowPerPage.ToString());
        if(hienthi==2)
            query = query.Replace("TenSP ASC", "ThoiGianDang DESC");
       // Response.Write(query);
        string sconnection = ConfigurationManager.ConnectionStrings["SMOConnectionString"].ConnectionString;
        System.Data.SqlClient.SqlConnection oconnection = new System.Data.SqlClient.SqlConnection(sconnection);
        oconnection.Open();
        System.Data.SqlClient.SqlCommand ocommand = new System.Data.SqlClient.SqlCommand();
        ocommand.CommandText = query;
        ocommand.Connection = oconnection;
        System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();
        adapter.SelectCommand = ocommand;
        DataTable table = new DataTable();
        adapter.Fill(table);
        //DataList_DanhSachSanPham.DataSource = spAdapter.SANPHAM_LayTatCa();
      //  DataList_DanhSachSanPham.DataSource = spAdapter.SANPHAM_LayTatCa_Paged(startIndex, RowPerPage);
        DataList_DanhSachSanPham.DataSource =null;
        DataList_DanhSachSanPham.DataSource = table;
        DataList_DanhSachSanPham.DataBind();
    }
    protected void ddlPhanTrang_SelectedIndexChanged(object sender, EventArgs e)
    {
     //   Response.Write(ddlPhanTrang.SelectedValue);
        Response.Redirect(GlobalFunctions.GetCurrentPageName()+"?rowperpage="+ ddlPhanTrang.SelectedValue);
    }
    protected void ddlThuTu_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["HienThi"] = int.Parse(ddlThuTu.SelectedValue);
        Response.Redirect(GlobalFunctions.GetCurrentPageName() + "?rowperpage=" + ddlPhanTrang.SelectedValue);
    }
}
